package com.cloudkinto.controller.order;


import cn.hutool.core.date.DateUtil;
import com.cloudkinto.base.BaseController;
import com.cloudkinto.bo.order.OrderMbQueryBo;
import com.cloudkinto.bo.order.OrderMbUpdateAccountSecretBo;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.common.DateUtils;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.service.order.OrderMabangService;
import com.cloudkinto.service.order.vo.mabang.MbAuthStorageVo;
import com.cloudkinto.service.shop.ShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 马帮mabang订单表(包含商品) 前端控制器
 * </p>
 *
 * @author hua
 * @since 2024-09-09
 */
@RestController
@Api(tags = "马帮mabang订单表(包含商品)模块")
@RequestMapping("/api/mabang")
public class OrderMabangController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(OrderMabangController.class);

    @Autowired
    private OrderMabangService service;

    @Autowired
    private ShopService shopService;


    @GetMapping("/addInit")
    @ApiOperation(value = "马帮mabang订单表(包含商品)新增初始化", notes = "马帮mabang订单表(包含商品)新增初始化")
    public SingleResult addInit() {
        return service.addInit();
    }

    @PostMapping("")
    @ApiOperation(value = "马帮mabang订单表(包含商品)新增", notes = "马帮mabang订单表(包含商品)新增")
    public SingleResult add(@RequestBody @Validated Object req) {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        return SingleResult.success("新增成功", service.add(req, userId));
    }

    @GetMapping("/updateInit/{id}")
    @ApiOperation(value = "马帮mabang订单表(包含商品)编辑初始化", notes = "马帮mabang订单表(包含商品)编辑初始化")
    public SingleResult updateInit(@PathVariable("id") Long id) {
        return service.updateInit(id);
    }

    @PutMapping("")
    @ApiOperation(value = "马帮mabang订单表(包含商品)修改", notes = "马帮mabang订单表(包含商品)修改")
    public SingleResult update(@RequestBody @Validated Object req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("修改成功", service.update(req, userId));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "马帮mabang订单表(包含商品)根据id删除", notes = "马帮mabang订单表(包含商品)根据id删除")
    public SingleResult delete(@PathVariable("id") Long id) {
        Long userId = getCurrentUserId();
        return SingleResult.success("删除成功", service.delete(id, userId));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "马帮mabang订单表(包含商品)根据id查看详情", notes = "马帮mabang订单表(包含商品)根据id查看详情")
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", service.detail(id));
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "马帮mabang订单表(包含商品)查询初始化", notes = "马帮mabang订单表(包含商品)查询初始化")
    public SingleResult pageInit() {
        return service.pageInit(getCompanyId());
    }

    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "马帮mabang订单表(包含商品)查询（分页）", notes = "马帮mabang订单表(包含商品)查询（分页）")
    public PageResult listPage(OrderMbQueryBo bo) {
        Long companyId = getCompanyId();
        bo.setCompanyId(companyId);
        return service.getListPage(bo);
    }

    @UserSessionRequired
    @GetMapping("/getMbAuthStorageList")
    @ApiOperation(value = "获取马帮mabang可以授权的仓库列表", notes = "获取马帮mabang可以授权的仓库列表")
    @ApiResponses({@ApiResponse(code = 200, message = "", response = MbAuthStorageVo.class)})
    public SingleResult getMbAuthStorageList() {
        return SingleResult.success(service.getMbAuthStorageList(getCompanyId()));
    }

//    @ApiOperation(value = "检查是否已经授权", notes = "检查是否已经授权")
//    @UserSessionRequired
//    @GetMapping("/checkAuth")
//    public SingleResult<Boolean> checkAuth() {
//        return service.checkAuth(getCompanyId());
//    }

    @UserSessionRequired
    @ApiOperation(value = "马帮mabang授权", notes = "马帮授权")
    @GetMapping("/authorize/{storageId}")
    public SingleResult<String> mbAuthorize(@PathVariable("storageId") Long storageId) {
        return service.mbAuthorize(getCompanyId(), storageId);
    }

    @UserSessionRequired
    @ApiOperation(value = "修改认证token", notes = "修改认证token")
    @PutMapping("/updateMbAccessToken")
    public SingleResult updateMbAccessToken(@RequestBody OrderMbUpdateAccountSecretBo bo) {
        return service.updateMbAccessToken(bo, getCompanyId());
    }

    @UserSessionRequired
    @ApiOperation(value = "同步订单", notes = "同步订单")
    @GetMapping("/syncOrder")
    public SingleResult syncOrder(String beforeDate, String afterDate) {
        Date before, after;
        if (StringUtils.isEmpty(beforeDate) || StringUtils.isEmpty(afterDate)) {
            // 默认查询5天
            before = DateUtils.passingDate(new Date(), -4);
            after = new Date();
        } else {
            before = DateUtil.parse(beforeDate);
            after = DateUtil.parse(afterDate);
        }
        return service.mbOrderSync(getCompanyId(), before, after);
    }

    @UserSessionRequired
    @ApiOperation(value = "同步商品到mabang", notes = "同步商品到mabang")
    @GetMapping("/sendHwcProduct")
    public SingleResult sendHwcProduct() {
        return service.sendHwcProductToMabang(getCompanyId());
    }
}

